package shanghaielectric.com.shanghaielectric.Communication.PortHelper;

import android.serialport.SerialPort;

import java.io.File;
import java.io.IOException;
import java.security.InvalidParameterException;

/**
 * Created by 45000460 on 2017/11/9.
 */

public class PortHelper {
    private static SerialPort mSerialPort = null;
    private static PortHelper portHelper;
    private PortHelper() {
        portHelper = new PortHelper();
    }

    public static SerialPort getSerialPort(int speed)
            throws SecurityException, IOException, InvalidParameterException {
        if (mSerialPort == null) {
            /* Read serial port parameters */

//            String packageName = mContext.getPackageName();
//            SharedPreferences sp = mContext.getSharedPreferences(packageName + "_preferences", MODE_PRIVATE);
            /**
             * //获得路径和波特率
             * */
//            String path = sp.getString("DEVICE", "");
//            int baudrate = Integer.decode(sp.getString("BAUDRATE", "-1"));

			/* Check parameters */
//            if ((path.length() == 0) || (baudrate == -1)) {
//                throw new InvalidParameterException();
//            }

			/* Open the serial port */
//            mSerialPort = new SerialPort(new File(path), baudrate, 0);


            mSerialPort = new SerialPort(new File("/dev/ttyS0"), speed, 0);
//            mSerialPort = new SerialPort(new File("/dev/ttyS2"), speed, 0);

        }
        return mSerialPort;
    }

    public static void closeSerialPort() {
        if (mSerialPort != null) {

            mSerialPort.close();
            mSerialPort = null;
        }
    }
}
